package arrAndstr;

import java.util.HashMap;
import java.util.Map;

/*
    多数元素
 */
public class T169 {

    public static void main(String[] args) {
        int[] array = new int[]{3,3,4};

        System.out.println(majorityElement(array));
    }

    public static int majorityElement(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();

        for(int num : nums) {
            if(!map.containsKey(num)) {
                map.put(num, 1);
            } else {
                map.put(num, map.get(num) + 1);
            }
        }

        int maxValue = 0;
        int maxCount = 0;
        for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() > maxCount) {
                maxCount = entry.getValue();
                maxValue = entry.getKey();
            }
        }

        return maxValue;
    }
}
